package cheeese.and.crackers;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Activity Class that will be displayed and run when program starts
 * @author Cheese and Crackers
 *
 */
public class CheeseandCrackersActivity extends Activity {
	
	private UserList users;
	private Button okButton;
	private Button newUserButton;
	private Button cleanUserListButton;
	
	/** 
	 * Called when the activity is first created. 
     * 
     * @param savedInstanceSate Saved state of activity when last suspended
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.userloginscreen);
        
    	users = new UserList();
    	try{
    		users.addUser(new User("default", "default", "default", "default"));
    	}
    	catch(Exception e){
    		Toast.makeText(getApplicationContext(), (CharSequence)(e.getMessage()), Toast.LENGTH_SHORT).show();
    	}
    	
        okButton = (Button)findViewById(R.id.buttonOK);
        newUserButton = (Button)findViewById(R.id.buttonNewUser);
        cleanUserListButton = (Button)findViewById(R.id.buttonCleanUserList);
        
        MainClickListener myListener = new MainClickListener();
        okButton.setOnClickListener(myListener);
        newUserButton.setOnClickListener(myListener);
        cleanUserListButton.setOnClickListener(myListener);
    }
    
   /**
    * Method to call when activity is paused
    */
    @Override
    public void onPause(){
	   super.onPause();
    	try{
    		users.save(getApplicationContext());
    	}
    	catch(Exception e){
    		Toast.makeText(getApplicationContext(), (CharSequence)("Fatal Error With User Storage - Shutting Down"), Toast.LENGTH_SHORT).show();
    		try {
    			Thread.sleep(2000);
    		} 
    		catch(InterruptedException e2) {} 
    		android.os.Process.killProcess(android.os.Process.myPid());
    	}
    }
    
    /**
     * Method to call when activity is resumed
     */
    @Override
    public void onResume(){
    	super.onResume();
    	try {
			users.load(getApplicationContext());
		} catch (Exception e) {
			try {
				users.save(getApplicationContext());
			}
			catch(Exception ex) {
				Toast.makeText(getApplicationContext(), (CharSequence)(e.getMessage() + "Fatal Error With User Storage - Shutting Down"), Toast.LENGTH_SHORT).show();
				try {
    			Thread.sleep(2000);
				} 
				catch(InterruptedException e2) {} 
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		}	
    }
    
    
    
    /**
     * Listener for button
     */
    public class MainClickListener implements OnClickListener {
    	@Override
    	/**
    	 * Checks which button was pressed and takes appropriate action, login or create user
    	 * @param v The view which was clicked
    	 */
		public void onClick(View v) {
    			if(v==okButton){
    				String loginId = ((EditText)findViewById(R.id.loginIdField)).getText().toString();
    				User userToDisplay = users.getUser(loginId);
    				
    	        	CharSequence charSequenceToDisplay;
    	        	boolean validated = false;
    	        	if(userToDisplay == null){
    	        		charSequenceToDisplay = "Invalid Login ID";
    	        	}
    	        	else{
    	        		String password = ((EditText)findViewById(R.id.passwordField)).getText().toString();
    	        		if(userToDisplay.checkPassword(password)){
    	        			charSequenceToDisplay = (CharSequence)("Name: " + userToDisplay.getName() + "\n" + "email: " + userToDisplay.getEmail() + "\n" + "login id: " + userToDisplay.getLoginId() + "\n" + "password: " + userToDisplay.getPassword());
    	        			validated = true;
    	        		}
    	        		else{
    	        			charSequenceToDisplay = (CharSequence)("Incorrect Password");
    	        		}
    	        	}
    	        	Toast.makeText(getApplicationContext(), charSequenceToDisplay, Toast.LENGTH_SHORT).show();
    	        	if(validated){
    	        		//Launch Task View of User
    	        		Intent taskViewIntent = new Intent(v.getContext(), TaskViewActivity.class);
    	             	taskViewIntent.putExtra("ACTIVE_USER", userToDisplay.getLoginId());
    	             	startActivity(taskViewIntent);
    	        	}
    			}	
    			else if(v==newUserButton){
    				Intent newUserIntent = new Intent(v.getContext(), NewUserActivity.class);
    				startActivity(newUserIntent);
    			}
    			else if(v==cleanUserListButton){
    				users = new UserList();
    				try{
    		    		users.addUser(new User("default", "default", "default", "default"));
    		    		users.save(getApplicationContext());
    		    	}
    		    	catch(Exception e){
    		    		Toast.makeText(getApplicationContext(), (CharSequence)(e.getMessage()), Toast.LENGTH_SHORT).show();
    		    	}
    			}
		}
    }
}

